package com.lishangshan.oas_system.controller.task;

import com.lishangshan.oas_system.model.dao.taskdao.TaskDao;
import com.lishangshan.oas_system.model.entity.task.Tasklist;
import com.lishangshan.oas_system.service.task.TaskService;
import com.lishangshan.oas_system.utils.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class MyTaskController {
    private int size = ParameterUtil.PAGE_SIZE_MIN;
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskDao taskDao;

    /**
     * 我的任务頁面初始化
     *
     * @param page
     * @param model
     * @param req
     * @param userId
     * @return
     */
    @RequestMapping("mytask")
    public String mytaskIndex(@RequestParam(value = "page", defaultValue = ParameterUtil.PAGE_NUMBER) int page, Model model
            , HttpServletRequest req, @SessionAttribute("userId") Long userId) {
        Page<Tasklist> tasklists = taskService.index3(userId, null, page, size);
        List<Map<String, Object>> list = taskService.index4(tasklists, userId);
        model.addAttribute("tasklist", list);
        model.addAttribute("page", tasklists);
        model.addAttribute("url", "mytaskIndexPaging");
        return "task/mytask";
    }

    /**
     * 分页
     *
     * @param page
     * @param model
     * @param title
     * @param userId
     * @return
     */
    @RequestMapping("mytaskIndexPaging")
    public String mytaskIndexPaging(@RequestParam(value = "page", defaultValue = ParameterUtil.PAGE_NUMBER) int page, Model model
            , @RequestParam(value = "title", required = false) String title, @SessionAttribute("userId") Long userId) {
        if (title == null) {
            title = "";
        }
        Page<Tasklist> tasklists = taskService.index3(userId, title, page, size);
        List<Map<String, Object>> list = taskService.index4(tasklists, userId);
        model.addAttribute("tasklist", list);
        model.addAttribute("page", tasklists);
        model.addAttribute("url", "mytaskIndexPaging");
        model.addAttribute("sort", "&title=" + title);
        return "task/mytasklist";
    }
}
